﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kaskelot.SP.Model.Client;
using Stockholm.SOA.Kaskelot.WCF.DataContracts;
using Kaskelot.SP.Core;
using Kaskelot.SP.Model.Client.Forms;

namespace Kaskelot.SP.Services.Client.Mapping
{
	public static class ClientPersonKursTillfalleExtensions
	{
		public static ClientPersonKursTillfalle ToClientContract(this PersonKursTillfalle entity)
		{
			ClientPersonKursTillfalle b = new ClientPersonKursTillfalle();

			CopyFromEntity(entity, b, false);

			return b;
		}

		public static ClientPersonKursTillfalle ToMinimalClientContract(this PersonKursTillfalle entity)
		{
			ClientPersonKursTillfalle b = new ClientPersonKursTillfalle();

			CopyFromEntity(entity, b, true);

			return b;
		}

		public static PersonKursTillfalle ToContract(this ClientPersonKursTillfalle b)
		{
			PersonKursTillfalle entity = new PersonKursTillfalle();

			CopyToEntity(b, entity);

			return entity;
		}

		private static void CopyFromEntity(PersonKursTillfalle from, ClientPersonKursTillfalle to, bool minimal)
		{
			to.Id = from.Id.ToString();
			to.ValId = from.ValId.ToString();

			to.Andrad = from.Andrad != null ? from.Andrad.ToString() : null;
			to.AndradAv = from.AndradAv;
			to.Skapad = from.Skapad != null ? from.Skapad.ToString() : null;
			to.SkapadAv = from.SkapadAv;

			to.PersonId = from.PersonId.HasValue ? from.PersonId.Value.ToString() : String.Empty;
			to.KursTillfalleId = from.KursTillfalleId.ToString();
			to.PersonNamn = from.PersonNamn;
			to.PersonEpost = from.PersonEpost;
			to.PersonUppdrag = from.PersonUppdrag;

			switch (from.Status)
			{
				case PersonKursTillfalle.StatusTyp.Narvaro:
					to.Status = "Narvaro";
					break;
				default:
					to.Status = "Placerad";
					break;
			}
		}

		private static void CopyToEntity(ClientPersonKursTillfalle from, PersonKursTillfalle to)
		{
			to.Id = new Guid(from.Id);
			to.ValId = new Guid(from.ValId);

			if (!String.IsNullOrEmpty(from.Andrad))
				to.Andrad = DateTime.Parse(from.Andrad);
			else
				to.Andrad = null;

			to.AndradAv = from.AndradAv;

			if (from.Skapad != null)
				to.Skapad = DateTime.Parse(from.Skapad);

			to.SkapadAv = from.SkapadAv;

			if (from.PersonId.IsGuid())
				to.PersonId = new Guid(from.PersonId);

			to.KursTillfalleId = new Guid(from.KursTillfalleId);

			switch (from.Status)
			{
				case "Narvaro":
					to.Status = PersonKursTillfalle.StatusTyp.Narvaro;
					break;
				default:
					to.Status = PersonKursTillfalle.StatusTyp.Placerad;
					break;
			}
		}
	}
}
